package member.member.action;

import m.common.model.util.ModelQueryList;
import m.server.web.AuthInfoUtil;
import m.system.cache.CacheUtil;
import m.system.exception.MException;
import m.system.util.ObjectUtil;
import member.base.action.BaseAction;
import member.base.model.BusinessInfo;
import member.base.model.MemberInfo;

public abstract class MemberAction extends BaseAction {
    /**
     * 登录设置当前用户
     */
    public void setSession(MemberInfo member){
        CacheUtil.push(AuthInfoUtil.getSession()+"_member",member.getOid());
    }
    /**
     * 获取当前登录用户 未登录则抛异常
     */
    public MemberInfo getSession() throws Exception {
        return getSession(true);
    }
    /**
     * 获取当前登录用户 isThrow
     */
    public MemberInfo getSession(boolean isThrow) throws Exception {
        return ModelQueryList.getModel(MemberInfo.class,getSessionOid(isThrow),"*","level.*","business.*");
    }
    /**
     * 获取当前登录用户oid 未登录则抛异常
     */
    public String getSessionOid() throws Exception {
        return getSessionOid(true);
    }
    /**
     * 获取当前登录用户oid isThrow
     */
    public String getSessionOid(boolean isThrow) throws Exception {
        Object member=CacheUtil.get(AuthInfoUtil.getSession()+"_member");
        if(null==member){
            if(isThrow) throw new MException("会员未登录");
            else return null;
        }else{
            return (String)member;
        }
    }
    /**
     * 判断数据是否归属当前登录会员
     */
    public void verifyMember(MemberInfo member) throws Exception {
        if(ObjectUtil.isEmptyOid(member))
            throw new MException("权限错误");
        if(!getSessionOid(true).equals(member.getOid())){
            throw new MException("权限错误");
        }
    }
}
